﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HM.TESTCS;
using HM.CS;

namespace HM
{
    public partial class InStockSourceBill : Form
    {
        //单据类型
        public int sourceType
        {
            get;
            set;
        }


        public InStockSourceBill()
        {
            InitializeComponent();
            SetFormWithAndHeith(this);
        }

        private void InStockSourceBill_Load(object sender, EventArgs e)
        {
            string srcBillTypeID;
            srcBillTypeID = "72";
            DataTable dt;
            dt = new ApploginTest().CreateInStockSrcBillInfo(srcBillTypeID);

            FillListView(dt, txtFilter.Text.Trim());
            switch (sourceType)
            {
                case 1:
                    lblSrcTypeName.Text = "外购入库单";
                    break;
                default:break;
            }
            

        }


        private void btnReset_Click(object sender, EventArgs e)
        {
            txtFilter.Text = "";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //该方法可用优化
            lvSourceBillInfo.Items.Clear();
            DataTable dt;
            string srcBillTypeID;
            srcBillTypeID = "72";
            dt = new ApploginTest().CreateInStockSrcBillInfo(srcBillTypeID);
            FillListView(dt, txtFilter.Text.Trim());
        }

        public void FillListView(DataTable dtSrcInfo, string filterText)
        {
            try
            {
                if (dtSrcInfo.Rows.Count > 0)
                {
                    DataRow[] dtSelect = dtSrcInfo.Select("srcBillNo like '%" + filterText + "%' or supplyName like '%" + filterText + "%'");

                    foreach (DataRow dr in dtSelect)
                    {
                        //日期
                        string date;
                        //源单编号
                        string srcBillNo;
                        //供应商
                        string supplyName;
                        //源单单据类型ID
                        string srcBillTypeID;


                        date = dr["date"].ToString();
                        srcBillNo = dr["srcBillNo"].ToString();
                        supplyName = dr["supplyName"].ToString();
                        srcBillTypeID = dr["srcBillTypeID"].ToString();
                        srcBillTypeID = dr["srcBillTypeID"].ToString();

                        ListViewItem lvi = new ListViewItem();
                        //首列赋值
                        lvi.Text = date;
                        lvi.ImageIndex = 0;
                        lvi.SubItems.Add(srcBillNo);
                        lvi.SubItems.Add(supplyName);
                        lvi.SubItems.Add(srcBillTypeID);

                        lvSourceBillInfo.Items.Add(lvi);
                    }
                    //结束数据处理，UI界面一次性绘制。
                    lvSourceBillInfo.EndUpdate();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("抱歉,错误的原因是:" + e.ToString());
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int intSelectIndex;
            int keyIndex;

            string date;
            string srcbillNo;
            string supply;
            string srcBillTypeID;
             
            if (lvSourceBillInfo.SelectedIndices.Count > 1)
            {
                MessageBox.Show("不能选择多条记录,请确认!");
                return;
            }
            else if (lvSourceBillInfo.SelectedIndices.Count < 1)
            {
                MessageBox.Show("请选择源单信息!");
                return;
            }

            intSelectIndex = lvSourceBillInfo.SelectedIndices[0];


            keyIndex = lvSourceBillInfo.Columns.IndexOf(colDate);
            date = lvSourceBillInfo.Items[intSelectIndex].SubItems[keyIndex].Text;

            keyIndex = lvSourceBillInfo.Columns.IndexOf(colSrcBillNo);
            srcbillNo = lvSourceBillInfo.Items[intSelectIndex].SubItems[keyIndex].Text;

            keyIndex = lvSourceBillInfo.Columns.IndexOf(colSupply);
            supply = lvSourceBillInfo.Items[intSelectIndex].SubItems[keyIndex].Text;

            keyIndex = lvSourceBillInfo.Columns.IndexOf(colSourceType);
            srcBillTypeID = lvSourceBillInfo.Items[intSelectIndex].SubItems[keyIndex].Text;



            InStockSrcBill obj = new InStockSrcBill();
            obj.Date = date;
            obj.BillNo = srcbillNo;
            obj.Supply = supply;
            obj.BillTypeID = srcBillTypeID;

            InStock.sfrmInStock.srcOBJ = obj;
            InStock.sfrmInStock.bRefresh = true;
            this.Hide();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void SetFormWithAndHeith(Form obj)
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }
    }
}